# This is a basic workflow to help you get started with Actions

name: CI

on:
  push:
    branches: [ master ]
    paths-ignore:
      - '.github/**'
      - 'docs/**'
      - 'docs-zh/**'
      - '**.md'
  pull_request:
    branches:
      - master
      - develop-*
      - blobstore-*
    paths-ignore:
      # - 'blobstore/**'
      # - '.github/**'
      # - 'docs/**'
      # - 'docs-zh/**'
      # - '**.md'

permissions:
  contents: read

jobs:
  ci-test-unit:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout repo
        uses: actions/checkout@c85c95e3d7251135ab7dc9ce3241c5835cc595a9 # v3.5.3

      - name: Find changed files
        id: changed-files
        uses: tj-actions/changed-files@87697c0dca7dd44e37a2b79a79489332556ff1f3 # v37.6.0
        with:
          files: |
            .github/**
            docs/**
            docs-zh/**
            **.md

      - name: Check gofmt
        if: steps.changed-files.outputs.only_changed != 'true'
        run: |
          docker/run_docker.sh --format

      - name: Unit tests
        if: steps.changed-files.outputs.only_changed != 'true'
        run: |
          docker/run_docker.sh --test

      - name: Upload codecov
        if: steps.changed-files.outputs.only_changed != 'true'
        uses: codecov/codecov-action@eaaf4bedf32dbdc6b720b63067d99c4d77d6047d # v3.1.4
        with:
          files: ./coverage.txt

  ci-test-s3:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout repo
        uses: actions/checkout@c85c95e3d7251135ab7dc9ce3241c5835cc595a9 # v3.5.3

      - name: Find changed files
        id: changed-files
        uses: tj-actions/changed-files@87697c0dca7dd44e37a2b79a79489332556ff1f3 # v37.6.0
        with:
          files: |
            .github/**
            docs/**
            docs-zh/**
            **.md

      - name: Remove pre-installed software to free up disk space
        if: steps.changed-files.outputs.only_changed != 'true'
        run: |
          sudo rm -rf /usr/local/lib/android

      - name: S3 tests
        if: steps.changed-files.outputs.only_changed != 'true'
        run: |
          docker/run_docker.sh --s3test

      - name: Pack docker_data
        if: ${{ always() && steps.changed-files.outputs.only_changed != 'true' }}
        run: |
            pushd docker
            sudo tar --exclude='docker_data/datanode*/disk' --exclude='docker_data/disk' -czvf docker_data.tar.gz docker_data
            popd

      - name: Upload docker_data.tar.gz
        if: ${{ always() && steps.changed-files.outputs.only_changed != 'true' }}
        uses: actions/upload-artifact@0b7f8abb1508181956e8e162db84b466c27e18ce # v3.1.2
        with:
          name: docker_data
          path: docker/docker_data.tar.gz
          retention-days: 7

  ci-sast:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout repository
        uses: actions/checkout@c85c95e3d7251135ab7dc9ce3241c5835cc595a9 # v3.5.3

      - name: Run Semgrep Security Scanner
        uses: returntocorp/semgrep-action@fcd5ab7459e8d91cb1777481980d1b18b4fc6735 # v1

      - name: Run Gosec Security Scanner
        run: |
          docker/run_docker.sh --gosec
